{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = \"\"\"\n",
    "---\n",
    "- name: Configure environment and build Skopeo\n",
    "  hosts: localhost\n",
    "  become: true  # 使用sudo执行任务\n",
    "  vars:\n",
    "    GO_VERSION: 1.21.0\n",
    "  tasks:\n",
    "    - name: Install required packages\n",
    "      apt:\n",
    "        name:\n",
    "          - libgpgme-dev\n",
    "          - libassuan-dev\n",
    "          - libbtrfs-dev\n",
    "          - pkg-config\n",
    "          - wget\n",
    "          - go-md2man \n",
    "        state: present\n",
    "\n",
    "    - name: Download and extract Go\n",
    "      unarchive:\n",
    "        src: \"https://golang.google.cn/dl/go{{ GO_VERSION }}.linux-amd64.tar.gz\"\n",
    "        dest: /usr/local\n",
    "        remote_src: yes\n",
    "\n",
    "    - name: Clone Skopeo repository (if not already cloned)\n",
    "      git:\n",
    "        repo: https://github.com/containers/skopeo.git\n",
    "        dest: \"src/github.com/containers/skopeo\"\n",
    "        clone: yes\n",
    "        update: yes\n",
    "\n",
    "    - name: Build Skopeo\n",
    "      make:\n",
    "        chdir: \"src/github.com/containers/skopeo\"\n",
    "        target: \"{{ item }}\"\n",
    "      with_items:\n",
    "        - bin/skopeo\n",
    "        - install\n",
    "      ignore_errors: yes\n",
    "\"\"\"\n",
    "import os\n",
    "import codecs\n",
    "import subprocess\n",
    "import requests\n",
    "\n",
    "os.environ[\"PKG_CONFIG_PATH\"] = \"/usr/lib/x86_64-linux-gnu/pkgconfig\"\n",
    "os.environ[\"GOPATH\"] = \"/kaggle/working\"\n",
    "os.environ[\"PATH\"] = os.environ.get(\"PATH\", \"\") + \":/usr/local/go/bin\"\n",
    "os.environ[\"LANG\"] = \"C.UTF-8\"\n",
    "os.environ[\"LC_ALL\"] = \"C.UTF-8\"\n",
    "\n",
    "with codecs.open(\"main.yml\", \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(data)\n",
    "\n",
    "URL = (\n",
    "    \"https://raw.githubusercontent.com/langgenius/dify/main/docker/docker-compose.yaml\"\n",
    ")\n",
    "CMD_TPL = \"skopeo copy --insecure-policy docker://{} oci-archive:images/{}.tar\"\n",
    "\n",
    "\n",
    "def get_images(url=URL, tpl=\"spo\", stop=\"qdran\"):\n",
    "    response = requests.get(url)\n",
    "    ret = []\n",
    "    used = []\n",
    "    if response.status_code == 200:\n",
    "        # 获取文件内容\n",
    "        file_content = response.text\n",
    "        for line in file_content.splitlines():\n",
    "            pline = line.lstrip()\n",
    "            if pline.startswith(\"image: \"):\n",
    "                img = pline.split()[-1]\n",
    "                if img not in used:\n",
    "                    ret.append(CMD_TPL.format(img, img.replace(\"/\", \"_\").replace(\":\", \"_\")))\n",
    "                    used.append(img)\n",
    "                    if stop:\n",
    "                        if stop in img:\n",
    "                            return ret\n",
    "        return ret\n",
    "\n",
    "!mkdir -p images\n",
    "!pip3 install ansible\n",
    "!ansible-playbook main.yml\n",
    "\n",
    "images = get_images()\n",
    "for cmd in images:\n",
    "    subprocess.run(cmd, shell=True)\n",
    "\n",
    "!tar -cvf downloads.tar images\n",
    "from IPython.display import FileLink\n",
    "FileLink(\"downloads.tar\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
